import requests
import json
import re


class User:
    API = {
        "login": "",
        "createFlow": "",
        "MaxChart": "",
        "mvTrash": "",
        "delete": "",
        "dolike": ""
    }

    def login(self):

        """
        登录网站
        :return:
        """
        postData = {
            'login_email': self.login_email,
            'login_password': self.login_password,
            'window': True
        }
        result = self.s.post(User.API['login'], data=postData)
        msg = json.loads(result.text)
        if msg['msg'] != "success":
            print("用户:%s【%s】账号或密码错误,请检查!!" % (self.login_email, self.login_password))
            exit(-1)

    def run(self, chartId):
        for index in range(0, 2):
            postData = {
                'chartId': chartId
            }
            result = self.s.post(User.API['dolike'], data=postData)
            msg = json.loads(result.text)
            if msg["result"]:
                # print("给 %s 点赞成功!" % chartId)
                break

    def expand(self):
        """
        开始扩容一次
        :return:
        """
        # 创建流程图
        result = currUser.createFlow()
        serchObj = re.search(r'var chartId = "([a-zA-Z0-9]*)"', result.text)
        # 找到图表的ID
        chartId = serchObj.group(1)
        # 点赞扩容
        for user in self.helperList:
            user.run(chartId)

        # 自己给自己点赞
        self.run(chartId)
        maxChart = self.getMaxChart()
        print("扩容完成,当前最大容量:%d" % maxChart)

        # 删除图表
        currUser.deleteChart(chartId)

    def setHelpList(self, helperList):
        """
        设置帮忙点赞人的列表
        :return:
        """
        self.helperList = helperList

    def createFlow(self):
        """
        创建流程图
        :return: api结果
        """
        return self.s.get(User.API['createFlow'])

    def getMaxChart(self):
        """
        获取最大容量
        :return:
        """
        result = self.s.get(User.API['MaxChart'])
        resultObj = json.loads(result.text)
        return resultObj['totalcount']

    def deleteChart(self, chartId):
        """
        删除自己的图表
        :return:
        """
        postData = {
            "fileType": "chart",
            "fileId": chartId,
            "resource": ""
        }
        # 删除创建的空流程图
        self.s.post(User.API['mvTrash'], data=postData)
        self.s.post(User.API['delete'], data=postData)

    def __init__(self, login_email, login_password):
        self.s = requests.Session()
        self.login_email = login_email
        self.login_password = login_password
        self.login()
        self.helperList = []
        # self.run()


if __name__ == "__main__":

    # 获取必要信息
    login_email = input("请输入您的账号:")
    login_password = input("请输入您的密码:")
    loginedUserList = []
    print("初始化必要资源...")
    # API地址
    with open("config/api.json") as config:
        api = config.read()
        User.API = json.loads(api)

    # 点赞用户
    with open("config/users.json") as config:
        configStr = config.read()
        userList = json.loads(configStr)
    for item in userList:
        user = User(item['name'], item['passwrod'])
        loginedUserList.append(user)

    print("用户 %s 开始登录..." % login_email)
    currUser = User(login_email, login_password)
    currUser.setHelpList(loginedUserList)
    # 开始扩容
    while True:
        currUser.expand()

pass
